<?php

	$user = "user";
	$pass = "pass";
	$dbname = "scheda_informativa";
	
	/*
	 * Output
	 */
	$output = array(
		"sEcho" => intval($_GET['sEcho']),
		"iTotalRecords" => 0,
		"iTotalDisplayRecords" => 0,
		"aaData" => array()
	);
	
	$sLimit = "";
	if ( isset( $_GET['iDisplayStart'] ) && $_GET['iDisplayLength'] != '-1' )
	{
		$sLimit = "LIMIT ". $_GET['iDisplayStart'] .", ". $_GET['iDisplayLength'];
	}
	
	$sWhere ="";
	if ( isset($_GET['sSearch']) && $_GET['sSearch'] != "" )
	{
		$sWhere = "WHERE (";
		$sWhere .= "Tipo LIKE '%".$_GET['sSearch']."%'";
		$sWhere .= ')';
	}
	try {
		$db = new PDO('mysql:host=localhost;dbname='.$dbname, $user, $pass);
		$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
		
		$rs = $db->query('SELECT count(*) from tabella');
		$iTotal = $rs->fetchColumn();
		$output['iTotalRecords']=$iTotal;
		$rs = $db->query("SELECT * from tabella  $sWhere $sLimit");
		$iFilteredTotal=$rs->rowCount();
		$output['iTotalDisplayRecords']=$iTotal;
		foreach($rs as $row) {
			$output['aaData'][] = array($row[0],$row[1],$row[2],$row[3]);
		}
		$db = null;
	} catch (PDOException $e) {
		print "Error!: " . $e->getMessage() . "<br/>";
		die();
	}
	
	
	echo json_encode( $output );
?>